package com.pomr.controller.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.data.AalData;
import com.pomr.service.common.AalDataService;
import com.pomr.util.common.IpMessage;

/**
 * 愛奧樂 数据上传接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/aal")
public class AalDataController {
	@Autowired
	@Qualifier("aalDataServiceImpl")
	private AalDataService aalDataService;
	
	@RequestMapping(value="/health",method=RequestMethod.GET)
	public String doHealthRecord(Model model){
		List<String> list = new ArrayList<String>();
		list.add("父亲");
		list.add("母亲");
		model.addAttribute("family", list);
		return "health/healthrecord";
	}
	
	@RequestMapping(value="/upload",method=RequestMethod.GET)
	@ResponseBody
	public String uploadData(String data,HttpServletRequest request){
		try {
			System.out.println("上传数据："+data);
			AalData ad = AalData.getInitializtion(data);
			if(!isSum(ad)){
				System.out.println("检校码错误");
				return "+IP"+IpMessage.getIpOf16(request)+IpMessage.getDateOf16(new Date())+"ER";
			}
			if(aalDataService.uploadData(ad)){
				System.out.println(IpMessage.getIpOf16(request));
				return "+IP"+IpMessage.getIpOf16(request)+IpMessage.getDateOf16(new Date())+"OK";
			}else{
				return "+IP"+IpMessage.getIpOf16(request)+IpMessage.getDateOf16(new Date())+"ER";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "+IP"+IpMessage.getIpOf16(request)+IpMessage.getDateOf16(new Date())+"ER";
		}
	}
	
	/**
	 * 检校
	 * @param ad
	 * @return
	 */
	public boolean isSum(AalData ad){
		System.out.println("开始检校");
		String str = ad.getDatastr();
		if(ad.getSum().equals(IpMessage.getSum(str))){
			System.out.println("检校码正确");
			return true;
		}
		return false;
	}
}
